package com.yhny.lykj.yuhannongye.Adapter;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Paint;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.longsichao.lscframe.view.LSCImageView;
import com.yhny.lykj.yuhannongye.R;
import com.yhny.lykj.yuhannongye.model.CarListModel;
import com.yhny.lykj.yuhannongye.tools.LCLog;

import java.util.List;


public class ShopcartOrderAdapter extends BaseExpandableListAdapter
{
	private Context context;
	private List<CarListModel.ListEntity> carList;

	/**
	 * 构造函数
	 *
	 * @param carList
	 *            组元素列表
	 * @param context
	 */
	public ShopcartOrderAdapter(List<CarListModel.ListEntity> carList, Context context)
	{
		super();
		this.carList = carList;
		this.context = context;
	}


	@Override
	public int getGroupCount()
	{
		return carList.size();
	}

	@Override
	public int getChildrenCount(int groupPosition)
	{
		return carList.get(groupPosition).goodslist.size();
	}

	@Override
	public Object getGroup(int groupPosition)
	{
		return carList.get(groupPosition);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition)
	{
		return carList.get(groupPosition).goodslist.get(childPosition);
	}

	@Override
	public long getGroupId(int groupPosition)
	{
		return 0;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition)
	{
		return 0;
	}

	@Override
	public boolean hasStableIds()
	{
		return false;
	}

	@Override
	public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
	{

		GroupHolder gholder;
		if (convertView == null)
		{
			gholder = new GroupHolder();
			convertView = View.inflate(context, R.layout.item_order_group, null);
			gholder.tv_group_name = (TextView) convertView.findViewById(R.id.tv_source_name);
			gholder.tv_group_price = (TextView) convertView.findViewById(R.id.item_order_price);
			//groupMap.put(groupPosition, convertView);
			 convertView.setTag(gholder);
		} else
		{
			// convertView = groupMap.get(groupPosition);
			gholder = (GroupHolder) convertView.getTag();
		}
		final CarListModel.ListEntity group = (CarListModel.ListEntity)getGroup(groupPosition);
		if (group != null)
		{
//			gholder.tv_group_name.setText(group.goodsid);


		}
		return convertView;
	}

	@Override
	public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
	{

		final ChildHolder cholder;
		if (convertView == null)
		{
			cholder = new ChildHolder();
			convertView = View.inflate(context, R.layout.item_order_product, null);
			cholder.imageView = (LSCImageView) convertView.findViewById(R.id.iv_adapter_list_pic);

			cholder.tv_price_line = (TextView) convertView.findViewById(R.id.item_price_line);
			cholder.tv_product_desc = (TextView) convertView.findViewById(R.id.tv_intro);
			cholder.tv_price = (TextView) convertView.findViewById(R.id.tv_price);
			cholder.iv_increase = (TextView) convertView.findViewById(R.id.tv_add);
			cholder.iv_decrease = (TextView) convertView.findViewById(R.id.tv_reduce);
			cholder.tv_count = (TextView) convertView.findViewById(R.id.tv_num);
			cholder.iv_increase.setVisibility(View.GONE);
			cholder.iv_decrease.setVisibility(View.GONE);
			// childrenMap.put(groupPosition, convertView);
			convertView.setTag(cholder);
		} else
		{
			// convertView = childrenMap.get(groupPosition);
			cholder = (ChildHolder) convertView.getTag();
		}
		final CarListModel.ListEntity.GoodsListEntity product = (CarListModel.ListEntity.GoodsListEntity) getChild(groupPosition, childPosition);

		if (product != null)
		{
			Resources resources = cholder.imageView.getContext().getResources();
			int height = resources.getDimensionPixelOffset(R.dimen.item_home_hot_height);
			int width = resources.getDimensionPixelOffset(R.dimen.item_home_hot_width);
			String imageUrl = "";
			if (product.goods_img!=null) {
				imageUrl = product.goods_img;
			}
			if (!TextUtils.isEmpty(product.goods_img)) {
				cholder.imageView.setPlaceHolder(R.drawable.normal_pic).setType(LSCImageView.Type.Normal).setTargetSize(width, height).setImageURI(Uri.parse(imageUrl));
			}else{
				cholder.imageView.setImageResource(R.drawable.normal_pic);
			}
			cholder.tv_product_desc.setText(product.goods_name);
			cholder.tv_price_line.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);//中间加横线

				cholder.tv_price_line.setText("￥"+product.price);
			cholder.tv_count.setText(product.goods_nums + "");

		}else{
			LCLog.e("product = null");
		}
		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition)
	{
		return false;
	}

	/**
	 * 组元素绑定器
	 * 
	 * 
	 */
	private class GroupHolder
	{
		TextView tv_group_name;
		TextView tv_group_price;
	}

	/**
	 * 子元素绑定器
	 * 
	 * 
	 */
	private class ChildHolder
	{

		LSCImageView imageView;

		TextView tv_product_name;
		TextView tv_price_line;
		TextView tv_product_desc;
		TextView tv_price;
		TextView iv_increase;
		TextView tv_count;
		TextView iv_decrease;
	}

}
